<?php
    /**
     *  
     */
    class Blog extends BaseModel
    {
        CONST STATUS_ALL        = -1;
        CONST STATUS_OPEN       = 1;
        CONST STATUS_CLOSE      = 2;
        CONST STATUS_DELETE     = 9;

        protected $_id;
        protected $_userId;
        protected $_name;
        protected $_description;
        protected $_status;
        protected $_createDate;
        protected $_updateDate;
        protected $_properties;
        protected $_normalizedSearch;

        /**
         *  讀取資料表而產生, 請視同cache, 任何情況下皆不可更動該產生的內容
         *  @return Array()
         */
        public static function getTableDefinition()
        {
        }

        /**
         *  經資料表傳至程式中時所對應的 method 資訊
         *  @return Array()
         */
        public static function getTableFields()
        {
            return Array(
                "id"                => "getId",
                "user_id"           => "getUserId",
                "name"              => "getName",
                "description"       => "getDescription",
                "status"            => "getStatus",
                "create_date"       => "getCreateDateByDb",
                "update_date"       => "getUpdateDateByDb",
                "properties"        => "getProperties",
                "normalized_search" => "getNormalizedSearch"
            );
        }

        /**
         *  reset to default value model
         *  @return Array()
         */
        public function resetValue() {
            $this->__sleep();
            $this->setId                   ( -1 );
            $this->setUserId               ( -1 );
            $this->setName                 ( NULL );
            $this->setDescription          ( NULL );
            $this->setStatus               ( Blog::STATUS_OPEN );
            $this->setCreateDate           ( time() );
            $this->setUpdateDate           ( time() );
            $this->setProperties           ( Array() );
            $this->setNormalizedSearch     ( NULL );
        }

        /**
         *  validate model data
         *  @return messages Array()
         */
        public function validate()
        {
            $validateInfo = parent::validate();

            if( !$this->getName() ) {
                $validateInfo['name']['message'] = '不能空值';
            }

            // timestamp
            if( $this->getCreateDate() && $this->getCreateDate() < -28800 ) {
                $validateInfo['createDate']['message'] = '日期不正確';
            }
            
            // timestamp
            if( $this->getUpdateDate() < -28800 ) {
                $validateInfo['updateDate']['message'] = '日期不正確';
            }
            
            return $validateInfo;
        }

        /**
         *  filter model data
         */
        public function filter()
        {
            $this->setName                 ( trim(strip_tags( $this->getName()                 )) );
            $this->setDescription          ( trim(strip_tags( $this->getDescription()          )) );
            $this->setStatus               ( (int) $this->getStatus() );
            $this->setNormalizedSearch     ( trim(strip_tags( $this->getNormalizedSearch()     )) );
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            basic method 
        ------------------------------------------------------------------------------------------------------------------------ */

        /**
         *  set user id
         */
        public function setUserId($int) 
        {
            $this->_userId = (int) $int;
        }
        /**
         *  get user id
         */
        public function getUserId() 
        {
            return $this->_userId;
        }

        /**
         *  get Name
         *  @return string
         */
        public function getName()
        {
            return $this->_name;
        }
        /**
         *  set Name
         */
        public function setName( $name )
        {
            $this->_name = trim($name);
        }

        /**
         *  get Description
         *  @return string
         */
        public function getDescription()
        {
            return $this->_description;
        }
        /**
         *  set Description
         */
        public function setDescription( $description )
        {
            $this->_description = trim($description);
        }

        /**
         *  get Status 
         *  @return int
         */
        public function getStatus()
        {
            return $this->_status;
        }
        /**
         *  set Status
         */
        public function setStatus( $status )
        {
            $this->_status = (int) $status;
        }

        /**
         *  get NormalizedSearch
         *  @return string
         */
        public function getNormalizedSearch()
        {
            return $this->_normalizedSearch;
        }
        /**
         *  set NormalizedSearch
         */
        public function setNormalizedSearch( $normalizedSearch )
        {
            $this->_normalizedSearch = trim($normalizedSearch);
        }


        /* ------------------------------------------------------------------------------------------------------------------------
            extends
        ------------------------------------------------------------------------------------------------------------------------ */
        /**
         *  clear extends information
         */
        public function __sleep()
        {
            $this->_user = null;
            return parent::__sleep();
        }

        /**
         *  get user
         *  @param isSave 是否儲存取得的物件
         *  @return object
         */
        public function getUser( $isSave=true )
        {
            if( !$isSave ) {
                $this->_user = null;
            }
            if( $this->_user ) {
                return $this->_user;
            }
            $users = new Users();
            $user = $users->getUser( $this->getUserId() );
            if( $isSave ) {
                $this->_user = $user;
            }
            return $user;
        }

    }

